<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文本" prop="value">
        <el-input
          v-model="queryParams.value"
          placeholder="请输入文本"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:lang:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:lang:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:lang:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="langList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="中文" align="center" prop="zh" />
      <el-table-column label="英文" align="center" prop="en" />
      <el-table-column label="越南文" align="center" prop="vi" />
      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:lang:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:lang:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改多语言配置对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="langRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" :disabled="title==='修改多语言'" />
        </el-form-item>
        <el-form-item label="中文" prop="zh">
          <el-input v-model="form.zh" placeholder="请输入中文" />
        </el-form-item>
        <el-form-item label="英文" prop="en">
          <el-input v-model="form.en" placeholder="请输入英文，双击自动翻译" @dblclick="handleTranslate('en')" />
        </el-form-item>
        <el-form-item label="越南文" prop="vi">
          <el-input v-model="form.vi" placeholder="请输入越南文，双击自动翻译" @dblclick="handleTranslate('vi')" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Lang">
import { listLang, getLang, delLang, saveLang, translateLang } from "@/api/system/lang";

const { proxy } = getCurrentInstance();

const langList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const codes = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: undefined,
    value: undefined
  },
  rules: {
    code: [{ required: true, message: "编码不能为空", trigger: "blur" }],
    zh: [{ required: true, message: "中文不能为空", trigger: "blur" }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询多语言配置列表 */
function getList() {
  loading.value = true;
  listLang(queryParams.value).then(response => {
    console.log(response);
    langList.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    code: undefined,
    zh: undefined,
    en: undefined,
    vi: undefined
  };
  proxy.resetForm("langRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  codes.value = selection.map(item => item.code);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加多语言";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = { ...row };
  open.value = true;
  title.value = "修改多语言";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["langRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        saveLang(form.value).then(response => {
          if (response.code === 200) {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          } else {
            proxy.$modal.msgError(response.msg);
          }
        });
      } else {
        saveLang(form.value).then(response => {
          if (response.code === 200) {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          } else {
            proxy.$modal.msgError(response.msg);
          }
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const langIds = row.id || ids.value;
  const langCodes = row.code || codes.value;
  proxy.$modal.confirm('是否删除编码为"' + langCodes + '"的数据项？').then(function() {
    return delLang(langIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 翻译按钮操作 */
function handleTranslate(type) {
  if (!form.value.zh) {
    proxy.$modal.msgError("请先输入中文内容");
    return;
  }
  translateLang({
    text: form.value.zh,
    sourceLang: "cn",
    targetLang: type
  }).then(response => {
    form.value[type] = response.data.translatedText;
  });
}

getList();
</script>